import geopandas as gpd
import matplotlib.pyplot as plt
#import folium
import folium
from folium import plugins
from folium.plugins import MeasureControl
import pandas as pd
import requests
import numpy as np
import pdvega # adds vgplot attribute to pandas
from vega import VegaLite
import json
import altair as alt
from altair import Chart, load_dataset
# load built-in dataset as a pandas DataFrame
cars = load_dataset("cars")
print(type(cars))
scatter = (
Chart(cars)
.mark_circle()
.encode(
x="Horsepower",
y="Miles_per_Gallon",
color="Origin",
)
)
vega = folium.features.VegaLite(
scatter,
width="100%",
height="100%",
)
m = folium.Map(location=[-27.5717, -48.6256]) #buat map dasar
marker = folium.features.Marker([-27.57, -48.62]) #buat marker
popup = folium.Popup() #buat wadah popup
vega.add_to(popup) #tambahkan chart vega ke popup
popup.add_to(marker) #tambahkan popup ke marker
marker.add_to(m)
plugins.LocateControl().add_to(m)
m
C:\Users\Gian Amanda\anaconda3\envs\geopandas_env\lib\site-packages\altair\utils\deprecation.py:65: AltairDeprecationWarning: load_dataset is deprecated. Use the vega_datasets package instead. warnings.warn(message, AltairDeprecationWarning)
<class 'pandas.core.frame.DataFrame'>
bandarlampung = gpd.read_file('Geospatial/bandarlampung/ADMINISTRASIKECAMATAN_AR_50K.shp')
#melihat data teratas
bandarlampung.head()
| KDPPUM | NAMOBJ | REMARK | KDPBPS | FCODE | LUASWH | UUPP | SRS_ID | LCODE | METADATA | ... | WADMKC | WIADKC | WADMKK | WIADKK | WADMPR | WIADPR | TIPADM | Shape_Leng | Shape_Area | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | None | PADANGCERMIN | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | PESAWARAN | None | LAMPUNG | None | 0 | 0.015246 | 9.276147e-06 | POLYGON Z ((105.27223 -5.51068 0.00000, 105.27... |
| 1 | None | PADANGCERMIN | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | PESAWARAN | None | LAMPUNG | None | 0 | 0.005070 | 9.054481e-07 | POLYGON Z ((105.26072 -5.48900 0.00000, 105.26... |
| 2 | None | TELUKBETUNG TIMUR | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.014730 | 9.877191e-06 | POLYGON Z ((105.26501 -5.46243 0.00000, 105.26... |
| 3 | None | KATIBUNG | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | LAMPUNG SELATAN | None | LAMPUNG | None | 0 | 0.959100 | 1.292074e-02 | MULTIPOLYGON Z (((105.34679 -5.52032 0.00000, ... |
| 4 | None | TELUKBETUNG TIMUR | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.173407 | 8.594191e-04 | POLYGON Z ((105.26360 -5.45600 0.00000, 105.26... |
5 rows × 28 columns
#create geodataframe
bandarlampung_gdf = gpd.GeoDataFrame(bandarlampung)
bandarlampung_gdf
| KDPPUM | NAMOBJ | REMARK | KDPBPS | FCODE | LUASWH | UUPP | SRS_ID | LCODE | METADATA | ... | WADMKC | WIADKC | WADMKK | WIADKK | WADMPR | WIADPR | TIPADM | Shape_Leng | Shape_Area | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | None | PADANGCERMIN | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | PESAWARAN | None | LAMPUNG | None | 0 | 0.015246 | 9.276147e-06 | POLYGON Z ((105.27223 -5.51068 0.00000, 105.27... |
| 1 | None | PADANGCERMIN | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | PESAWARAN | None | LAMPUNG | None | 0 | 0.005070 | 9.054481e-07 | POLYGON Z ((105.26072 -5.48900 0.00000, 105.26... |
| 2 | None | TELUKBETUNG TIMUR | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.014730 | 9.877191e-06 | POLYGON Z ((105.26501 -5.46243 0.00000, 105.26... |
| 3 | None | KATIBUNG | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | LAMPUNG SELATAN | None | LAMPUNG | None | 0 | 0.959100 | 1.292074e-02 | MULTIPOLYGON Z (((105.34679 -5.52032 0.00000, ... |
| 4 | None | TELUKBETUNG TIMUR | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.173407 | 8.594191e-04 | POLYGON Z ((105.26360 -5.45600 0.00000, 105.26... |
| 5 | None | BUMIWARAS | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.152226 | 3.618440e-04 | POLYGON Z ((105.29204 -5.42908 0.00000, 105.29... |
| 6 | None | TELUKBETUNG BARAT | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.263935 | 1.468368e-03 | POLYGON Z ((105.23724 -5.42979 0.00000, 105.23... |
| 7 | None | PANJANG | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.349980 | 1.089657e-03 | POLYGON Z ((105.32931 -5.45278 0.00000, 105.32... |
| 8 | None | TELUKBETUNG SELATAN | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.164935 | 2.857965e-04 | POLYGON Z ((105.24855 -5.42530 0.00000, 105.24... |
| 9 | None | TELUKBETUNG UTARA | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.113610 | 3.545772e-04 | POLYGON Z ((105.25885 -5.42600 0.00000, 105.25... |
| 10 | None | PADANGCERMIN | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | PESAWARAN | None | LAMPUNG | None | 0 | 1.276866 | 3.122260e-02 | POLYGON Z ((105.18844 -5.43036 0.00000, 105.18... |
| 11 | None | ENGGAL | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.090174 | 2.276263e-04 | POLYGON Z ((105.26980 -5.42017 0.00000, 105.26... |
| 12 | None | TANJUNGKARANG PUSAT | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.109039 | 2.813376e-04 | POLYGON Z ((105.26026 -5.40259 0.00000, 105.26... |
| 13 | None | TANJUNGKARANG TIMUR | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.086104 | 1.713920e-04 | POLYGON Z ((105.26893 -5.40876 0.00000, 105.26... |
| 14 | None | SUKABUMI | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.210214 | 2.045593e-03 | POLYGON Z ((105.32315 -5.39628 0.00000, 105.32... |
| 15 | None | MERBAUMATARAM | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | LAMPUNG SELATAN | None | LAMPUNG | None | 0 | 0.795404 | 1.073303e-02 | MULTIPOLYGON Z (((105.34887 -5.51011 0.00000, ... |
| 16 | None | KEDAMAIAN | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.163759 | 6.799901e-04 | POLYGON Z ((105.28896 -5.39318 0.00000, 105.28... |
| 17 | None | WAYLIMA | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | PESAWARAN | None | LAMPUNG | None | 0 | 0.632092 | 8.971683e-03 | POLYGON Z ((105.15454 -5.47544 0.00000, 105.15... |
| 18 | None | TANJUNGKARANG BARAT | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.208336 | 9.325419e-04 | POLYGON Z ((105.24710 -5.38407 0.00000, 105.24... |
| 19 | None | KEDATON | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.095843 | 3.089224e-04 | POLYGON Z ((105.27196 -5.38068 0.00000, 105.27... |
| 20 | None | LANGKAPURA | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.123152 | 4.343658e-04 | POLYGON Z ((105.24630 -5.38204 0.00000, 105.24... |
| 21 | None | WAYHALIM | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.120973 | 5.075422e-04 | POLYGON Z ((105.27661 -5.37292 0.00000, 105.27... |
| 22 | None | KEMILING | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.228773 | 1.757273e-03 | POLYGON Z ((105.21607 -5.36929 0.00000, 105.21... |
| 23 | None | SUKARAME | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.152851 | 8.967914e-04 | POLYGON Z ((105.30755 -5.36019 0.00000, 105.30... |
| 24 | None | LABUHANRATU | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.116432 | 4.955390e-04 | POLYGON Z ((105.25065 -5.35812 0.00000, 105.25... |
| 25 | None | TANJUNGSENANG | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.152406 | 7.470007e-04 | POLYGON Z ((105.29070 -5.34806 0.00000, 105.29... |
| 26 | None | GEDONGTATAAN | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | PESAWARAN | None | LAMPUNG | None | 0 | 0.617663 | 1.269139e-02 | MULTIPOLYGON Z (((105.20690 -5.38728 0.00000, ... |
| 27 | None | RAJABASA | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.222881 | 1.053251e-03 | POLYGON Z ((105.26089 -5.32809 0.00000, 105.26... |
| 28 | None | TANJUNGBINTANG | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | LAMPUNG SELATAN | None | LAMPUNG | None | 0 | 0.752524 | 1.387638e-02 | POLYGON Z ((105.34452 -5.43371 0.00000, 105.34... |
| 29 | None | JATIAGUNG | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | LAMPUNG SELATAN | None | LAMPUNG | None | 0 | 1.047365 | 1.940891e-02 | MULTIPOLYGON Z (((105.29253 -5.35083 0.00000, ... |
| 30 | None | NATAR | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | LAMPUNG SELATAN | None | LAMPUNG | None | 0 | 1.051700 | 2.152723e-02 | MULTIPOLYGON Z (((105.24889 -5.33292 0.00000, ... |
31 rows × 28 columns
#melihat data terbawah
bandarlampung.tail()
| KDPPUM | NAMOBJ | REMARK | KDPBPS | FCODE | LUASWH | UUPP | SRS_ID | LCODE | METADATA | ... | WADMKC | WIADKC | WADMKK | WIADKK | WADMPR | WIADPR | TIPADM | Shape_Leng | Shape_Area | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 26 | None | GEDONGTATAAN | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | PESAWARAN | None | LAMPUNG | None | 0 | 0.617663 | 0.012691 | MULTIPOLYGON Z (((105.20690 -5.38728 0.00000, ... |
| 27 | None | RAJABASA | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | BANDARLAMPUNG | None | LAMPUNG | None | 0 | 0.222881 | 0.001053 | POLYGON Z ((105.26089 -5.32809 0.00000, 105.26... |
| 28 | None | TANJUNGBINTANG | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | LAMPUNG SELATAN | None | LAMPUNG | None | 0 | 0.752524 | 0.013876 | POLYGON Z ((105.34452 -5.43371 0.00000, 105.34... |
| 29 | None | JATIAGUNG | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | LAMPUNG SELATAN | None | LAMPUNG | None | 0 | 1.047365 | 0.019409 | MULTIPOLYGON Z (((105.29253 -5.35083 0.00000, ... |
| 30 | None | NATAR | Wilayah Administrasi Kecamatan | None | None | 0.0 | None | None | BA0080 | None | ... | None | None | LAMPUNG SELATAN | None | LAMPUNG | None | 0 | 1.051700 | 0.021527 | MULTIPOLYGON Z (((105.24889 -5.33292 0.00000, ... |
5 rows × 28 columns
#melihat struktur geodataframe
bandarlampung.info()
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 31 entries, 0 to 30 Data columns (total 28 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 KDPPUM 0 non-null object 1 NAMOBJ 31 non-null object 2 REMARK 31 non-null object 3 KDPBPS 0 non-null object 4 FCODE 0 non-null object 5 LUASWH 31 non-null float64 6 UUPP 0 non-null object 7 SRS_ID 0 non-null object 8 LCODE 31 non-null object 9 METADATA 0 non-null object 10 KDEBPS 0 non-null object 11 KDEPUM 0 non-null object 12 KDCBPS 0 non-null object 13 KDCPUM 0 non-null object 14 KDBBPS 0 non-null object 15 KDBPUM 0 non-null object 16 WADMKD 0 non-null object 17 WIADKD 0 non-null object 18 WADMKC 0 non-null object 19 WIADKC 0 non-null object 20 WADMKK 31 non-null object 21 WIADKK 0 non-null object 22 WADMPR 31 non-null object 23 WIADPR 0 non-null object 24 TIPADM 31 non-null int64 25 Shape_Leng 31 non-null float64 26 Shape_Area 31 non-null float64 27 geometry 31 non-null geometry dtypes: float64(3), geometry(1), int64(1), object(23) memory usage: 6.9+ KB
#manampilkan summary statistic
bandarlampung.describe()
| LUASWH | TIPADM | Shape_Leng | Shape_Area | |
|---|---|---|---|---|
| count | 31.0 | 31.0 | 31.000000 | 3.100000e+01 |
| mean | 0.0 | 0.0 | 0.337638 | 4.720350e-03 |
| std | 0.0 | 0.0 | 0.357225 | 7.867549e-03 |
| min | 0.0 | 0.0 | 0.005070 | 9.054481e-07 |
| 25% | 0.0 | 0.0 | 0.115021 | 3.317498e-04 |
| 50% | 0.0 | 0.0 | 0.164935 | 8.594191e-04 |
| 75% | 0.0 | 0.0 | 0.483822 | 5.508638e-03 |
| max | 0.0 | 0.0 | 1.276866 | 3.122260e-02 |
#investigasi duplikasi nama data data non-numerik
bandarlampung['NAMOBJ'].value_counts()
PADANGCERMIN 3 TELUKBETUNG TIMUR 2 JATIAGUNG 1 TANJUNGBINTANG 1 RAJABASA 1 GEDONGTATAAN 1 TANJUNGSENANG 1 LABUHANRATU 1 SUKARAME 1 KEMILING 1 WAYHALIM 1 LANGKAPURA 1 KEDATON 1 TANJUNGKARANG BARAT 1 WAYLIMA 1 KEDAMAIAN 1 MERBAUMATARAM 1 SUKABUMI 1 TANJUNGKARANG TIMUR 1 TANJUNGKARANG PUSAT 1 ENGGAL 1 TELUKBETUNG UTARA 1 TELUKBETUNG SELATAN 1 PANJANG 1 TELUKBETUNG BARAT 1 BUMIWARAS 1 KATIBUNG 1 NATAR 1 Name: NAMOBJ, dtype: int64
#mengetahui luas area terbesar
bandarlampung.loc[bandarlampung['Shape_Area'].idxmax()]#luas area terbesar
KDPPUM None NAMOBJ PADANGCERMIN REMARK Wilayah Administrasi Kecamatan KDPBPS None FCODE None LUASWH 0.0 UUPP None SRS_ID None LCODE BA0080 METADATA None KDEBPS None KDEPUM None KDCBPS None KDCPUM None KDBBPS None KDBPUM None WADMKD None WIADKD None WADMKC None WIADKC None WADMKK PESAWARAN WIADKK None WADMPR LAMPUNG WIADPR None TIPADM 0 Shape_Leng 1.276866 Shape_Area 0.031223 geometry POLYGON Z ((105.18844153600013 -5.430357782999... Name: 10, dtype: object
#mengetahui luas area terkecil
bandarlampung.loc[bandarlampung['Shape_Area'].idxmin()]#luas area terkecil
#disini terlihat padang cermin karena memiliki 3 wilayah, dan satu dari wilayahnya merupakan luasan terkecil
KDPPUM None NAMOBJ PADANGCERMIN REMARK Wilayah Administrasi Kecamatan KDPBPS None FCODE None LUASWH 0.0 UUPP None SRS_ID None LCODE BA0080 METADATA None KDEBPS None KDEPUM None KDCBPS None KDCPUM None KDBBPS None KDBPUM None WADMKD None WIADKD None WADMKC None WIADKC None WADMKK PESAWARAN WIADKK None WADMPR LAMPUNG WIADPR None TIPADM 0 Shape_Leng 0.00507 Shape_Area 0.000001 geometry POLYGON Z ((105.26072472300005 -5.489001772999... Name: 1, dtype: object
#plotting luas wilayah dengan matplotlib scatterplot
fig, ax = plt.subplots(figsize=(14,6))
ax.scatter(bandarlampung['NAMOBJ'],bandarlampung['Shape_Area'])
ax.set_xlabel('Kecamatan')
plt.xticks(rotation='vertical')
ax.set_ylabel('Luas Wilayah')
Text(0, 0.5, 'Luas Wilayah')
#check missing value
print('Check kolom yang memiliki missing data:')
print(bandarlampung.isnull().any())
Check kolom yang memiliki missing data: KDPPUM True NAMOBJ False REMARK False KDPBPS True FCODE True LUASWH False UUPP True SRS_ID True LCODE False METADATA True KDEBPS True KDEPUM True KDCBPS True KDCPUM True KDBBPS True KDBPUM True WADMKD True WIADKD True WADMKC True WIADKC True WADMKK False WIADKK True WADMPR False WIADPR True TIPADM False Shape_Leng False Shape_Area False geometry False dtype: bool
#Handling missing value by imputation
print("Before imputation: \n")
#Checking amount of missing value
print(bandarlampung.isnull().sum())
#Counting missing values
print(bandarlampung.isnull().sum().sum())
print('Ukuran bandarlampung sebelum di drop: \n', bandarlampung.shape)
print('\n Mengembalikan Keys dari list: \n', list(bandarlampung.keys()))
Before imputation: KDPPUM 31 NAMOBJ 0 REMARK 0 KDPBPS 31 FCODE 31 LUASWH 0 UUPP 31 SRS_ID 31 LCODE 0 METADATA 31 KDEBPS 31 KDEPUM 31 KDCBPS 31 KDCPUM 31 KDBBPS 31 KDBPUM 31 WADMKD 31 WIADKD 31 WADMKC 31 WIADKC 31 WADMKK 0 WIADKK 31 WADMPR 0 WIADPR 31 TIPADM 0 Shape_Leng 0 Shape_Area 0 geometry 0 dtype: int64 558 Ukuran bandarlampung sebelum di drop: (31, 28) Mengembalikan Keys dari list: ['KDPPUM', 'NAMOBJ', 'REMARK', 'KDPBPS', 'FCODE', 'LUASWH', 'UUPP', 'SRS_ID', 'LCODE', 'METADATA', 'KDEBPS', 'KDEPUM', 'KDCBPS', 'KDCPUM', 'KDBBPS', 'KDBPUM', 'WADMKD', 'WIADKD', 'WADMKC', 'WIADKC', 'WADMKK', 'WIADKK', 'WADMPR', 'WIADPR', 'TIPADM', 'Shape_Leng', 'Shape_Area', 'geometry']
#Handling missing value by dropping
print("After Dropping:")
#Drop non numerical object with missing value
bandarlampung = bandarlampung.dropna(axis='columns')#menghapus missing value pada columns
print('\n Mengembalikan Keys dari list setelah didrop:\n', list(bandarlampung.keys()))
print('\n Ukuran bandarlampung setelah didrop:', bandarlampung.shape)
bandarlampung.head()
After Dropping: Mengembalikan Keys dari list setelah didrop: ['NAMOBJ', 'REMARK', 'LUASWH', 'LCODE', 'WADMKK', 'WADMPR', 'TIPADM', 'Shape_Leng', 'Shape_Area', 'geometry'] Ukuran bandarlampung setelah didrop: (31, 10)
| NAMOBJ | REMARK | LUASWH | LCODE | WADMKK | WADMPR | TIPADM | Shape_Leng | Shape_Area | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | PADANGCERMIN | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | PESAWARAN | LAMPUNG | 0 | 0.015246 | 9.276147e-06 | POLYGON Z ((105.27223 -5.51068 0.00000, 105.27... |
| 1 | PADANGCERMIN | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | PESAWARAN | LAMPUNG | 0 | 0.005070 | 9.054481e-07 | POLYGON Z ((105.26072 -5.48900 0.00000, 105.26... |
| 2 | TELUKBETUNG TIMUR | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | BANDARLAMPUNG | LAMPUNG | 0 | 0.014730 | 9.877191e-06 | POLYGON Z ((105.26501 -5.46243 0.00000, 105.26... |
| 3 | KATIBUNG | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | LAMPUNG SELATAN | LAMPUNG | 0 | 0.959100 | 1.292074e-02 | MULTIPOLYGON Z (((105.34679 -5.52032 0.00000, ... |
| 4 | TELUKBETUNG TIMUR | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | BANDARLAMPUNG | LAMPUNG | 0 | 0.173407 | 8.594191e-04 | POLYGON Z ((105.26360 -5.45600 0.00000, 105.26... |
#Cek Korelasi Data
print('Korelasi Shape_Leng dengan Shape_Area')
print(bandarlampung[['Shape_Leng', 'Shape_Area']].corr())
Korelasi Shape_Leng dengan Shape_Area
Shape_Leng Shape_Area
Shape_Leng 1.0000 0.9638
Shape_Area 0.9638 1.0000
#cek koordinat shp
bandarlampung.crs
<Compound CRS: EPSG:9518> Name: WGS 84 + EGM2008 height Axis Info [ellipsoidal|vertical]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) - H[up]: Gravity-related height (metre) Area of Use: - name: World. - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 ensemble - Ellipsoid: WGS 84 - Prime Meridian: Greenwich Sub CRS: - WGS 84 - EGM2008 height
#ubah sistem koordinat referensi
bandarlampung = bandarlampung.to_crs(epsg=4326)
bandarlampung.crs
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World. - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 ensemble - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
#plot pada peta
bandarlampung.plot()
<AxesSubplot:>
#menambahkan parameter cmap(color map)
bandarlampung.plot(cmap='jet')
<AxesSubplot:>
#parameter column untuk kategori pewarnaan, figsize untuk ukuran plot
bandarlampung.plot(cmap='jet', column = 'NAMOBJ', figsize = (10,10))
<AxesSubplot:>
#convert save shp ke GeoJSON
bandarlampung.to_file('Geospatial/bandarlampung/ADMINISTRASIKECAMATAN_AR_50K.geojson', driver='GeoJSON')
#open file geoJSON
bdl_geojson = r'Geospatial/bandarlampung/ADMINISTRASIKECAMATAN_AR_50K.geojson'
#buat gdf dari file GeoJSON
bdl_geogdf = gpd.read_file(bdl_geojson, driver='GeoJSON')
#open file json
bdl_json = r'Geospatial/bandarlampung/ADMINISTRASIKECAMATAN_AR_50K.json'
#buat gdf dari file json
bdl_gdf = gpd.read_file(bdl_json, driver='TopoJSON')
#Prepare GeoDataFrame for Altair
#dump as json
bdl_gdf_json = bdl_gdf.to_json()
# load as a GeoJSON object.
bdl_json_features = json.loads(bdl_gdf_json)
#parse variable `features` from json_features to `alt.Data`
bdl_data_geo = alt.Data(values=bdl_json_features['features'])
#Next we grab the states and put them in a list and check the length.
bdl_geogdf_list = list(bdl_geogdf.NAMOBJ.values)
len(bdl_geogdf_list)
31
bdl_geogdf.info()
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 31 entries, 0 to 30 Data columns (total 10 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 NAMOBJ 31 non-null object 1 REMARK 31 non-null object 2 LUASWH 31 non-null float64 3 LCODE 31 non-null object 4 WADMKK 31 non-null object 5 WADMPR 31 non-null object 6 TIPADM 31 non-null int64 7 Shape_Leng 31 non-null float64 8 Shape_Area 31 non-null float64 9 geometry 31 non-null geometry dtypes: float64(3), geometry(1), int64(1), object(5) memory usage: 2.5+ KB
# plot map, where variables ares nested within `properties`,
alt.Chart(bdl_data_geo).mark_bar(
fill='#023e8a',
stroke='ffffff'
).properties(
projection={'type': 'albersUsa'},
width=500,
height=225
).encode(
alt.Y('properties.Shape_Area', type='quantitative'),
alt.X('properties.NAMOBJ', type='nominal'),
color=('properties.Shape_Area:Q')
)
# plot map, where variables ares nested within `properties`,
alt.Chart(bdl_data_geo).mark_circle(
fill='#023e8a',
stroke='ffffff',
size=60
).properties(
projection={'type': 'albersUsa'},
width=500,
height=225
).encode(
alt.Y('properties.Shape_Area', type='quantitative'),
alt.X('properties.NAMOBJ', type='nominal'),
color=('properties.Shape_Area:Q'),
tooltip=['properties.NAMOBJ:nominal','properties.Shape_Area:Q']
).interactive()
# plot map, where variables ares nested within `properties`,
grafik = alt.Chart(bdl_data_geo).mark_point().properties(
width=500,
height=225
).encode(
x='properties.NAMOBJ:N',
y='properties.Shape_Area:Q',
tooltip=['properties.NAMOBJ:nominal','properties.Shape_Area:Q']
).interactive()
alt.vconcat(
grafik.encode(color='properties.Shape_Area:N').properties(title='Nominal'),
)
#Buat peta dan marker graphic
from altair import Chart, load_dataset
import branca
#buat map dasar
m = folium.Map(location=[-5.443251942283236, 105.27673629553274],
zoom_start=10,
tiles = 'Stamen Color',
attr = 'Esri',
name = 'Esri Satellite',
control_scale = True
)
#buat style function
#load GeoJSON
folium.GeoJson(
bdl_json_features,
zoom_on_click=True,
style_function=lambda feature: {
"fillColor": "feature.properties.NAMOBJ",
"color": "black",
"weight": 2,
"dashArray": "5, 5",
},
name="Load GeoJSON",
).add_to(m)
folium.Choropleth(
bdl_geogdf,
data=bdl_geogdf,
columns=['NAMOBJ',"Shape_Area"], #Pembagian Berdasarkan Warna dan Legend
key_on="feature.properties.NAMOBJ",
bins=6,
fill_color='YlGnBu',
nan_fill_color='White',
fill_opacity=1,
nan_fill_opacity=None,
line_color='Black',
line_weight=1,
line_opacity=0.4,
name="Administrasi Bandarlampung",
tooltip=['NAMOBJ','Shape_Area'],
legend_name='Luas Area',
overlay=True,
control=True,
show=True,
topojson=None,
smooth_factor=0,
highlight=True
).add_to(m)
#buat grafik
scatter = alt.Chart(bdl_data_geo).mark_bar(
fill='lightgray',
stroke='white'
).properties(
projection={'type': 'albersUsa'},
width=300,
height=150
).encode(
alt.Y('properties.Shape_Area:Q'),
alt.X('properties.NAMOBJ:N'),
alt.Color('properties.Shape_Area:Q')
)
vega = folium.features.VegaLite(
scatter,
width="100%",
height="100%",
)
#buat marker
marker = folium.features.Marker([-5.443251942283236, 105.27673629553274]) #buat marker
popup = folium.Popup() #buat wadah popup
vega.add_to(popup) #tambahkan chart vega ke popup
popup.add_to(marker) #tambahkan popup ke marker
marker.add_to(m)
m.add_child(MeasureControl(position="topleft"))
#membuat minimap menjadi fullscreen
plugins.Fullscreen(position='topleft').add_to(m)
#menggambar/interaktif map
draw = plugins.Draw(
export=True,
position="topleft"
)
draw.add_to(m)
#untuk menampilkan lonlang
m.add_child(folium.LatLngPopup())
plugins.LocateControl().add_to(m)
# We add a layer controller.
folium.LayerControl(collapsed=True).add_to(m)
m
#buka shp bandarlampung untuk overlay
area_of_interest = gpd.read_file('Geospatial/bandarlampung/areainterest/area_of_interest.shp')
area_of_interest.head()
| id | geometry | |
|---|---|---|
| 0 | 1 | POLYGON ((105.20086 -5.45080, 105.20042 -5.435... |
area_of_interest.crs
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World. - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 ensemble - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
area_of_interest.plot()
<AxesSubplot:>
# color without fill, edgecolor = outline
cmap='hsv'
fig, ax = plt.subplots(figsize = (10,10))
bandarlampung.plot(ax = ax, cmap = cmap, edgecolor = 'black', column = 'NAMOBJ', legend=True)
#create Title
plt.title(f'Area of Interest Bandar Lampung: {cmap}')
#plot overlay kecamatan wayhalim dengan warna putih
area_of_interest.plot(ax = ax, color = 'none', edgecolor = 'white', linewidth=3)
<AxesSubplot:title={'center':'Area of Interest Bandar Lampung: hsv'}>
# Intersecting Layers
bandarlampung_in_aoi = gpd.overlay(bandarlampung, area_of_interest, how = 'intersection', keep_geom_type=True)
bandarlampung_in_aoi.plot(edgecolor = 'red', figsize=(8,8))
bandarlampung_in_aoi.head()
| NAMOBJ | REMARK | LUASWH | LCODE | WADMKK | WADMPR | TIPADM | Shape_Leng | Shape_Area | id | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | TELUKBETUNG TIMUR | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | BANDARLAMPUNG | LAMPUNG | 0 | 0.173407 | 0.000859 | 1 | POLYGON Z ((105.22685 -5.46197 13.30533, 105.2... |
| 1 | BUMIWARAS | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | BANDARLAMPUNG | LAMPUNG | 0 | 0.152226 | 0.000362 | 1 | MULTIPOLYGON Z (((105.29204 -5.42911 13.59653,... |
| 2 | TELUKBETUNG BARAT | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | BANDARLAMPUNG | LAMPUNG | 0 | 0.263935 | 0.001468 | 1 | POLYGON Z ((105.23758 -5.42994 13.43351, 105.2... |
| 3 | PANJANG | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | BANDARLAMPUNG | LAMPUNG | 0 | 0.349980 | 0.001090 | 1 | POLYGON Z ((105.29119 -5.43090 13.58509, 105.2... |
| 4 | TELUKBETUNG SELATAN | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | BANDARLAMPUNG | LAMPUNG | 0 | 0.164935 | 0.000286 | 1 | MULTIPOLYGON Z (((105.26530 -5.44116 13.45749,... |
# geometry as boundary
bandarlampung_in_aoi.geometry.name
'geometry'
#buat column didalam bandarlampung_in_aoi
bandarlampung_in_aoi['centroid_column'] = bandarlampung_in_aoi.centroid
bdl_bulat = bandarlampung_in_aoi.set_geometry('centroid_column')
bdl_bulat.plot()
C:\Users\GIANAM~1\AppData\Local\Temp/ipykernel_26976/3736743472.py:2: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation. bandarlampung_in_aoi['centroid_column'] = bandarlampung_in_aoi.centroid
<AxesSubplot:>
# select area BUMIWARAS
bandarlampung_in_aoi_bw=bandarlampung_in_aoi[bandarlampung_in_aoi['NAMOBJ'] == 'BUMIWARAS']
# explore as map
bandarlampung_in_aoi_bw.explore(column='NAMOBJ',
cmap='Set2',
legend=False,
tooltip=['NAMOBJ','Shape_Area'])
# explore as overlay map
m = bandarlampung.explore(
column='NAMOBJ',
name="Administrasi Bandar Lampung",
cmap='Set2',
#legend=False,
tooltip=['NAMOBJ','Shape_Area'],
#tiles='Stamen Terrain',
style_kwds=dict(color="white",weight=2)
)
#buat overlay point of interest
bdl_bulat.explore(
m=m, # pass the map object
color="red", # use red color on all points
marker_kwds=dict(radius=10, fill=True), # make marker radius 10px with fill
tooltip="NAMOBJ", # show "name" column in the tooltip
tooltip_kwds=dict(labels=False), # do not show column label in the tooltip
name="Area of Interests" # name of the layer in the map
)
folium.TileLayer('Stamen Toner', control=True).add_to(m)
folium.LayerControl().add_to(m)
#membuat minimap menjadi fullscreen
plugins.Fullscreen(position='topleft').add_to(m)
#menggambar/interaktif map
draw = plugins.Draw(
export=True,
position="topleft"
)
draw.add_to(m)
#untuk menampilkan lonlang
m.add_child(folium.LatLngPopup())
# check mark point
bdl_bulat.head()
| NAMOBJ | REMARK | LUASWH | LCODE | WADMKK | WADMPR | TIPADM | Shape_Leng | Shape_Area | id | geometry | centroid_column | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | TELUKBETUNG TIMUR | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | BANDARLAMPUNG | LAMPUNG | 0 | 0.173407 | 0.000859 | 1 | POLYGON Z ((105.22685 -5.46197 13.30533, 105.2... | POINT (105.23649 -5.46761) |
| 1 | BUMIWARAS | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | BANDARLAMPUNG | LAMPUNG | 0 | 0.152226 | 0.000362 | 1 | MULTIPOLYGON Z (((105.29204 -5.42911 13.59653,... | POINT (105.28954 -5.43049) |
| 2 | TELUKBETUNG BARAT | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | BANDARLAMPUNG | LAMPUNG | 0 | 0.263935 | 0.001468 | 1 | POLYGON Z ((105.23758 -5.42994 13.43351, 105.2... | POINT (105.22609 -5.44255) |
| 3 | PANJANG | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | BANDARLAMPUNG | LAMPUNG | 0 | 0.349980 | 0.001090 | 1 | POLYGON Z ((105.29119 -5.43090 13.58509, 105.2... | POINT (105.29386 -5.42862) |
| 4 | TELUKBETUNG SELATAN | Wilayah Administrasi Kecamatan | 0.0 | BA0080 | BANDARLAMPUNG | LAMPUNG | 0 | 0.164935 | 0.000286 | 1 | MULTIPOLYGON Z (((105.26530 -5.44116 13.45749,... | POINT (105.24559 -5.43200) |
from shapely.geometry import Point
# create marker manually
#buat dictionary for marker
d = {'name': ['Marker1'], 'geometry': [Point(105.27673629553274,-5.443251942283236)]}
#buat GeoDataFrame
marker_gdf = gpd.GeoDataFrame(d, crs="EPSG:4326") #buat marker dari gdf
marker_gdf
#convert to json
#marker_json = marker_gdf.to_json()
| name | geometry | |
|---|---|---|
| 0 | Marker1 | POINT (105.27674 -5.44325) |
# create overlay map using explore
mo = folium.Map(location=[-5.443251942283236, 105.27673629553274],
zoom_start=12,
tiles = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
attr = 'Esri',
name = 'Esri Satellite',
control_scale = True
)
m = bandarlampung.explore(
column='NAMOBJ',
name="Administrasi Bandar Lampung",
cmap='Set2',
#legend=False,
tooltip=['NAMOBJ','Shape_Area'],
#tiles='Stamen Terrain',
style_kwds=dict(color="white",weight=2)
)
#Create blue marker
marker_gdf.explore(
m=m,
color="blue",
marker_kwds=dict(radius=5, fill=True), # make marker radius 10px with fill
tooltip="name", # show "name" column in the tooltip
tooltip_kwds=dict(labels=False), # do not show column label in the tooltip
name="Area of Int" # name of the layer in the map
)
#buat overlay point of interest
bdl_bulat.explore(
m=m, # pass the map object
color="red", # use red color on all points
marker_kwds=dict(radius=10, fill=True), # make marker radius 10px with fill
tooltip="NAMOBJ", # show "name" column in the tooltip
tooltip_kwds=dict(labels=False), # do not show column label in the tooltip
name="Area of Interests" # name of the layer in the map
)
folium.TileLayer('Stamen Toner', control=True).add_to(m)
folium.LayerControl().add_to(m)
#membuat minimap menjadi fullscreen
plugins.Fullscreen(position='topleft').add_to(m)
#menggambar/interaktif map
draw = plugins.Draw(
export=True,
position="topleft"
)
draw.add_to(m)
#untuk menampilkan lonlang
m.add_child(folium.LatLngPopup())
bandarlampung_in_aoi.explore(column='NAMOBJ',
cmap='Set2',
legend=False,
tooltip=['NAMOBJ','Shape_Area'],
tiles='Stamen Terrain',
style_kwds=dict(color="white",weight=2)
)
# create folium map
m2 = folium.Map(location=[-5.443251942283236, 105.27673629553274],
zoom_start=12,
tiles = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
attr = 'Esri',
name = 'Esri Satellite',
control_scale = True
)
#Create Measure Control
m2.add_child(MeasureControl(position="topright"))
#membuat minimap menjadi fullscreen
plugins.Fullscreen(position='topleft').add_to(m2)
#menggambar/interaktif map
draw = plugins.Draw(
export=True,
position="topleft"
)
draw.add_to(m2)
#untuk menampilkan lonlang
m2.add_child(folium.LatLngPopup())
m2
#Create Choropleth Map
import pandas as pd
folium.Choropleth(
geo_data=bandarlampung,
name="choropleth",
data=bandarlampung,
columns=["NAMOBJ", "Shape_Area"],
key_on="feature.id",
fill_color="YlGnBu",
fill_opacity=0.7,
line_color = "white",
line_opacity=0.2,
legend_name="Luas Area",
).add_to(m)
m